<template>
  <div>
    <f-form ref='form' :action='api.payment' :form-key='this.formItem.paymentId' :handlerName='handlerName'>
      <div>
        <f-form-item label='结算金额' name='amount' disabled :value.sync='formItem.amount' />
        <f-form-item label='开票单位' placeholder='请选择开票单位' name='invoiceUnitName' :readOnly='true' :required='true' :value.sync='formItem.invoiceUnitName' :trigger-change='true'>
          <a-tooltip slot='addonBefore' placement='top' title='点击图标选择开票单位'>
            <a-icon type='team' style='cursor:pointer' @click='openInvoiceModal' />
          </a-tooltip>
        </f-form-item>
        <f-form-item label='发票类型' type='radio' :required='true' data-type='number' name='invoiceType' :value.sync='formItem.invoiceType' :data='api.invoiceunit.invoiceType' />
        <f-form-item label='是否包票' type='radio' :required='true' data-type='number' name='baopiao' :value.sync='formItem.baopiao' :data='api.invoiceunit.baopiaoType' />
        <f-form-item label='申请日期' type='date' name='amountDate' :required='true' :value='formItem.amountDate' />
        <f-form-item label='发票项目' name='title' :value.sync='formItem.title' />
        <f-form-item label='备注' name='remark' :value='formItem.remark' />
      </div>
    </f-form>
    <select-invoice-modal ref='invoiceModal' @onSelectChange='onSelectInvoiceChange'></select-invoice-modal>
  </div>
</template>
<script>

import api from "../../../../js/api"
import SelectInvoiceModal from "@/views/module/haiying/components/select_invoice_modal"

export default {
  name: "InvoiceApplyForm",
  components: { SelectInvoiceModal },
  data() {
    let me = this
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      loaded: false,
      handlerName: "",
      selectIds: "",
      paymentId: "",
      formItem: this.getFormItem()
    }
  },
  created() {
  },
  computed: {},
  methods: {
    getFormItem() {
      return {
        deptSettleInvoiceId: "",
        companyId: "",
        invoiceTitle: "",
        invoiceUnitId: null,
        taxAmount: 0,
        invoiceAmount: 0,
        amount: 0,
        selectIds: null,
        settleApplyId: null,
        amountDate: this.$moment().format("YYYY-MM-DD"),
        invoiceType: 2,
        baopiao: 1,
        remark: ""
      }
    },
    setData(data) {
      this.$refs.form.resetFields()
      this.formItem = this.getFormItem()
      //结算单ID
      this.settleApplyId = data.settleApplyId
      this.selectIds = data.selectIds
      this.paymentId = data.paymentId
      this.handlerName = "hyInvoicePaymentSaveHandler"
      if (this.selectIds) {
        this.http.get(this, {
          url: this.api.payment.getInvoiceInfo,
          params: {
            settleIds: data.selectIds
          },
          success: (me, data) => {
            this.$nextTick(() => {
              this.formItem = data
              this.formItem.amount = data.totalSettleAmount
              this.loaded = true
            })
          }
        })
      }
      if (this.paymentId) {
        this.http.get(this, {
          url: this.api.payment.get,
          params: {
            paymentId: data.paymentId,
            handlerName: "hyInvoicePayInfoGetHandler"
          },
          success: (me, data) => {
            this.$nextTick(() => {
              this.formItem = data
            })
          }
        })
      }
    },
    slotObjChange(value) {
      this.formItem.amountFormulaContent = value
    },
    /**
     * 选择开票单位
     */
    openInvoiceModal() {
      this.$refs["invoiceModal"].open()
    },
    /**
     * 开票单位选择回调
     */
    onSelectInvoiceChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "invoiceUnitName": selectedRows[0].invoiceunitName })
      this.formItem.invoiceUnitId = selectedRowKeys[0]
      this.formItem.contractDate = selectedRows[0].contractDate
      this.$forceUpdate()
    },
    handleSubmit(callback,beforeSubmit) {
      this.$refs["form"].submit({
        invoiceUnitId: this.formItem.invoiceUnitId,
        selectIds: this.selectIds
      }, this.handleSuccess,beforeSubmit)
    },
    handleSuccess() {
      this.$emit("closeDrawer", false)
    }
  }
}
</script>
